/*
 * @Description:
 * @Author: fhw
 * @Date: 2023-02-22 17:04:39
 */
import { Modal, message } from 'ant-design-vue'
import dayjs from 'dayjs'
import { curry } from 'ramda'
import { createVNode } from 'vue'
import { ExclamationCircleOutlined } from '@ant-design/icons-vue'
import { DebouncedFunc } from './tools/throttle'
type Func = DebouncedFunc<(params: ObjectMap, options?: ObjectMap) => Promise<any>>
export const valueFormat = 'YYYY-MM-DD HH:mm:ss'
export const mobileReg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
export const rangePickerShowTime = {
	defaultValue: [dayjs('00:00:00', 'HH:mm:ss'), dayjs('23:59:59', 'HH:mm:ss')],
}
/**
 *@desc: 数组中含有对象，判断值在不在数组的对象中
 * @param {any[]} arr
 * @param {string} key
 * @param {(string | number)} value
 * @return { boolean} 结果
 */
export function arrIsKey(arr: any[], key: string, value: string | number) {
	let result = false
	result = arr.some((item: any) => {
		if (item[key] == value) {
			return true
		}
	})
	return result
}
//提示信息优化
function notificationTips(status: 'success' | 'error', description: string) {
	message[status](description)
}
const curryTips = curry(notificationTips)
export const successTips = curryTips('success')
export const errorTips = curryTips('error')
export interface FilterOption {
	children: string
	key: string
	value: string | number
	title?: string
	label?: string
}
/** 不区分大小写 */
export function filterOptionCaseInsensitive(inputValue: string, option: FilterOption) {
	const label = option.title ?? option.label ?? option.children
	return label.toUpperCase().includes(inputValue.toUpperCase())
}

export const dateFormat = 'YYYY-MM-DD HH:mm'

export const defaultParams = {
	pageSize: 10,
	page: 1,
}
/**
 * 
 * @param params 删除数据
 * @param request 删除接口
 * @param callback 删除成功回调
 */
export function deleteData<T extends (...arg: any) => Promise<any>>(params: ObjectMap, request: T, callback?: Function) {
	Modal.confirm({
		title: '是否确定删除该数据？',
		icon: createVNode(ExclamationCircleOutlined),
		onOk: async () => {
			const { success } = await request(params)
			if (success) {
				successTips('删除成功')
				callback && callback()
			}
		},
	})
}

export const pipeFormatJsonHtml = (str?: string) => {
	if (str) {
		return formatJsonHtml(JSON.parse(str))
	}
	return ''
}
function formatJsonHtml(msg: any) {
	const rep = '~'
	let jsonStr = JSON.stringify(msg, null, rep)
	let str = ''
	for (let i = 0; i < jsonStr.length; i++) {
		const text2 = jsonStr.charAt(i)
		if (i > 1) {
			const text = jsonStr.charAt(i - 1)
			if (rep != text && rep == text2) {
				str += '<br/>'
			}
		}
		str += text2
	}
	jsonStr = ''
	for (let i = 0; i < str.length; i++) {
		const text = str.charAt(i)
		if (rep == text) jsonStr += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
		else {
			jsonStr += text
		}
		if (i == str.length - 2) jsonStr += '<br/>'
	}
	return jsonStr
}

// json转对象
export function jsonToObj(json: string): any {
	let obj: ObjectMap = {}
	if (typeof json === 'string') {
		obj = JSONParse(json)
	} else {
		obj = json
	}
	for (const key in obj) {
		if (typeof obj[key] === 'string') {
			obj[key] = JSONParse(obj[key])
		}
	}
	return obj
}

// json转换
function JSONParse(json: string) {
	try {
		return eval('(' + json + ')')
	} catch (error) {
		return json
	}
}